<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <#include "/common/_commresource.html"/>
    <link rel="stylesheet" type="text/css" href="${request.contextPath}/static/summernote/summernote.css"></link>
 	<title>发帖子</title>
</head>
  <body>
    <#include "/common/_commtop.html"/> 
<section class="content-wrap">
<div class="container">
<div class="row">
            
<main class="col-md-12 main-content">
<article class="post">
	<div class="template-page-wrapper">
	<form class="form-horizontal templatemo-signin-form" role="form">
		<!-- 标题start -->
		<div class="form-group">
			<div class="col-md-12">
				<label for="name" class="col-sm-2 control-label">标题</label>
				<div class="col-sm-8">
				<input type="text" class="form-control"
									id="section_name" name="section_name" placeholder="标题">
				</div>
				<div class="col-sm-2"></div>
			</div>
		</div>
		<!-- 标题end -->
		
		<!-- 内容start -->
		<div class="form-group">
			<div class="col-sm-1"></div>
			<div class="col-md-10">
				<div id="summernote">在此输入帖子内容</div>
			</div>
			<div class="col-sm-1"></div>
		</div>
<script src="${request.contextPath}/static/summernote/summernote.js"></script>	
		<!-- 内容end -->
		<!-- operation 操作按钮 -->
		<div class="form-group">
			<div class="col-md-4">
			</div>
			<div class="col-md-4" >
    		<button type="button" class="btn btn-primary" style="width:60%; margin-right:5%;" onclick="saveSection();">发表</button>
    		<a class="btn btn-warning" onclick="self.location=document.referrer;" role="button" style="width:30%;">返回</a> 
			</div>
			<div class="col-md-4">
			</div>
		</div>
		<!-- ooperation 按钮结束 -->
        
	</form>
	</div>
</article>
</main>
            </div>
        </div>
    </section>
<!-- 页脚 start -->
<#include "/common/_friendshiplink.html"/>
<!-- 页脚 end -->
</body>
 <script>
 $(document).ready(function() {
     $('#summernote').summernote();
 });
 $('#summernote').summernote({  
 	lang: 'zh-CN',
 	minHeight: 300,             // set minimum height of editor
	  maxHeight: null,             // set maximum height of editor
     callbacks: {  
         onImageUpload: function(files) { //the onImageUpload API  
             console.debug(files);
             img = sendFile(files[0]);  
     }  
 }  
 });  

 function sendFile(file) {  
     data = new FormData();  
     data.append("file", file);  
     console.log(data);  
     $.ajax({  
         data: data,  
         type: "POST",  
         url: "${request.contextPath}/upload-file/image",  
         cache: false,  
         contentType: false,  
         processData: false,  
         success: function(data) {  
             if(data.status==200){
             	$("#summernote").summernote('insertImage',data.path, 'image name');
             }else{
					alert(data.error);
             }
         }  
     });  
 }  
	function getCode(){
		var markupStr = $('#summernote').summernote('code');
		return markupStr;
	}
	function setCode(markupStr){
		$('#summernote').summernote('code', markupStr);
	}
	function saveSection(){
		var dataJson = $("#navbar_login").serialize();
		$.ajax({
		    type: "post",
		    dataType: 'json',
		    url: '${request.contextPath}/section/save',
		    data: {name:$("#section_name").val(),context:getCode()},
		    success:function(data){
		    	if(data.status==200){
		    		layer.msg(data.msg, {icon: 1,time:2000});
		    		location.replace("${request.contextPath}/");
			    }else{
			    	layer.tips(data.msg, '#section_name',{tips: 3});
				}
		    }
		});
	}
	//setCode('');
  </script>
</html>